package com.ete.service.dialog

import android.app.Activity
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import androidx.appcompat.app.AppCompatDialog
import com.ete.service.util.ServiceUtil
import com.ete.service.util.log.EteLog


/**
 * Author:LiXian
 * <p>
 * Desc:TODO
 * Version:1.0
 * date 2024/1/15 16:44
 */
abstract class BaseDialog(private var mContext: Context, mStyle: Int)
    : AppCompatDialog(mContext, mStyle) {

    override fun onStart() {
        super.onStart()
        val width = ServiceUtil.getDialogWidth(getDialogWidthRate())
        if (window != null) {
            val mLayoutParams = window!!.attributes
            mLayoutParams.width = width
            mLayoutParams.height = mLayoutParams.height
            window!!.attributes = mLayoutParams
            window!!.setBackgroundDrawable(BitmapDrawable())
        }
    }

    override fun show() {
        if (!isFinished()) {
            ServiceUtil.disableFocus(window)
            super.show()
            if (isFullScreen()) {
                ServiceUtil.fullScreen(window)
            }
            ServiceUtil.enableFocus(window)
        } else {
            EteLog.d(BaseDialog::class.simpleName, "Activity已经结束")
        }
    }

    override fun dismiss() {
        if (isFinished()) {
            EteLog.d(BaseDialog::class.simpleName, "Activity已经结束")
        } else {
            super.dismiss()
        }
    }

    private fun isFinished(): Boolean {
        if (mContext is Activity) {
            val mActivity = mContext as Activity
            return mActivity.isFinishing || mActivity.isDestroyed
        }
        return true
    }

    open fun isFullScreen(): Boolean {
        return true
    }

    abstract fun getDialogWidthRate(): Float
}